package net.sushine.dc.mongodbsync.util;

/**
 * @author zhang
 * @version 1.0
 * @date 2024-12-09 16:20
 */
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.QueryOperators;
import java.util.ArrayList;
import java.util.List;

/* loaded from: mongodb-sync-1.0.jar:BOOT-INF/classes/net/sushine/dc/mongodbsync/util/MongoDBUtil.class */
public class MongoDBUtil {
    public static List<BasicDBObject> getMaxTS(String unitId) {
        List<BasicDBObject> queryList = new ArrayList<>();
        BasicDBObject query = new BasicDBObject();
        BasicDBObject[] array = {new BasicDBObject("unitId", unitId)};
        query.append(QueryOperators.AND, (Object) array);
        BasicDBObject match = new BasicDBObject("$match", query);
        queryList.add(match);
        BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("timestamp", -1));
        BasicDBObject limit = new BasicDBObject("$limit", 1);
        queryList.add(sort);
        queryList.add(limit);
        return queryList;
    }

    public static List<BasicDBObject> getPageQuery(int currentPageSize, int pageSize, int pageNo, long timestamp, String unitId) {
        List<BasicDBObject> queryList = new ArrayList<>();
        if (timestamp != -1) {
            BasicDBObject query = new BasicDBObject();
            BasicDBObject[] array = {new BasicDBObject("timestamp", new BasicDBObject(QueryOperators.GT, Long.valueOf(timestamp))), new BasicDBObject("unitId", unitId)};
            query.append(QueryOperators.AND, (Object) array);
            BasicDBObject match = new BasicDBObject("$match", query);
            queryList.add(match);
        }
        BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("timestamp", 1));
        BasicDBObject skip = new BasicDBObject("$skip", Integer.valueOf((pageNo - 1) * pageSize));
        BasicDBObject limit = new BasicDBObject("$limit", Integer.valueOf(currentPageSize));
        queryList.add(sort);
        queryList.add(skip);
        queryList.add(limit);
        return queryList;
    }

    public static List<BasicDBObject> count() {
        List<BasicDBObject> queryList = new ArrayList<>();
        BasicDBObject group = new BasicDBObject("$group", new BasicDBObject(DBCollection.ID_FIELD_NAME, "$unitId").append("count", (Object) new BasicDBObject("$sum", 1)));
        queryList.add(group);
        return queryList;
    }

    public static List<BasicDBObject> countSource(long maxTimestamp) {
        List<BasicDBObject> queryList = new ArrayList<>();
        BasicDBObject query = new BasicDBObject();
        BasicDBObject[] array = {new BasicDBObject("timestamp", new BasicDBObject(QueryOperators.GT, Long.valueOf(maxTimestamp)))};
        query.append(QueryOperators.AND, (Object) array);
        BasicDBObject match = new BasicDBObject("$match", query);
        queryList.add(match);
        BasicDBObject group = new BasicDBObject("$group", new BasicDBObject(DBCollection.ID_FIELD_NAME, "$unitId").append("count", (Object) new BasicDBObject("$sum", 1)));
        queryList.add(group);
        return queryList;
    }
}
